package ac.kr.incheon.android_gietae;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class RestaurantHandler extends DefaultHandler {
	private List<RestaurantXmlDataModel> dataResultList = new ArrayList<RestaurantXmlDataModel>();
	private RestaurantXmlDataModel model;
	private StringBuffer time = new StringBuffer();
	private StringBuffer name = new StringBuffer();
	private StringBuffer price = new StringBuffer();
	private StringBuffer date = new StringBuffer();

	private String curDay = "";
	private boolean hasTime = false;
	private boolean hasName = false;
	private boolean hasPrice = false;
	private boolean hasDate = false;

	/*startElement(java.lang.String uri, java.lang.String localName, 
	 * java.lang.String qName, Attributes atts) 
       Receive notification of the beginning of an element.*/
	public void startElement(String uri, String localName, String qName, Attributes atts) {
		if (localName.equals("Time")) {
			hasTime = true;
		} else if (localName.equals("Name")) {
			hasName = true;
		} else if (localName.equals("Price")) {
			hasPrice = true;
		} else if (localName.equals("Date")) {
			hasDate = true;
			date = new StringBuffer();
		} else if (localName.equals("items")) {
			time = new StringBuffer();
			name = new StringBuffer();
			price = new StringBuffer();
			date = new StringBuffer();			
			model = new RestaurantXmlDataModel();
		}
	}
    /*
     * endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) 
          Receive notification of the end of an element.
     */
	public void endElement(String uri, String localName, String qName) {

		 Calendar now = Calendar.getInstance();
		 String getCurDate = now.getTime().toString();
		 String curDate=getCurDate.substring(8,10);
		
		if ("Date".equals(localName)) {
			curDay = date.substring(0, 2);
		} else if ("items".equals(localName)) {
			if (curDay.equals(curDate)) {
				if (localName.equals("items")) {
					model.setStrTime(time.toString());
					model.setStrPrice(price.toString());
					model.setStrName(name.toString());
					dataResultList.add(model);
				}
			}
		}
	}

	/* Receive notification of character data.*/
	public void characters(char[] chars, int start, int leng) {
		if (hasTime) {
			hasTime = false;
			time.append(chars, start, leng);
		} else if (hasName) {
			hasName = false;
			name.append(chars, start, leng);
		} else if (hasPrice) {
			hasPrice = false;
			price.append(chars, start, leng);
		} else if (hasDate) {
			hasDate = false;
			date.append(chars, start, leng);
		}

	}

	public RestaurantXmlDataModel getModel() {
		return model;
	}

	public void setModel(RestaurantXmlDataModel model) {
		this.model = model;
	}

	public List<RestaurantXmlDataModel> getRestaurantXmlDataModelList() {
		return dataResultList;
	}

	public void setUsefulFirstRestaurantXmlDataModelList(List<RestaurantXmlDataModel> UsefulFirstRestaurantXmlDataModelList) {
		this.dataResultList = UsefulFirstRestaurantXmlDataModelList;
	}

}